#include "mc_aux.h"

struct VS_INPUT
{
    uint z8_y8_x8_null4_edge4 : TEXCOORD0;
	uint nVertexID               : SV_VERTEXID;
    
};
struct VS_OUTPUT
{
    float4 projCoord          : SV_POSITION;
	uint2  vertexID_and_slice : TEXCOORD0;
    
};
struct GS_OUTPUT {
	float4 projCoord    : SV_POSITION;
	uint   vertexID     : TEXCOORD0;
	uint   RTIndex      : SV_RenderTargetArrayIndex;
};

struct PS_INPUT {
	float4 projCoord    : SV_POSITION;
	uint   vertexID     : TEXCOORD0;
};
VS_OUTPUT VS( VS_INPUT input )
{
	uint  edgeNum = input.z8_y8_x8_null4_edge4 & 0x0F;
	int3 xyz = (int3)((input.z8_y8_x8_null4_edge4.xxx >> uint3(8,16,24)) & 0xFF);
	
	xyz.x *= 3;
	if (edgeNum==1)
	  xyz.x += 0;
	if (edgeNum==2)
	  xyz.x += 1;
	if (edgeNum==4)
	  xyz.x += 2;

	float2 uv = (float2)xyz.xy;
	//float texelSize = 1.0f/33.0f;

	VS_OUTPUT output;
	output.projCoord.x  = ((uv.x + 0.5f)*texelSize / 3.0f )*2 - 1;  //-1..1 range
	output.projCoord.y  = ((uv.y + 0.5f)*texelSize  )*2 - 1;  //-1..1 range
	// fix upside-down projection:
	output.projCoord.y *= -1;
	output.projCoord.z  = 0;
	output.projCoord.w  = 1;
	output.vertexID_and_slice.x = input.nVertexID;
	output.vertexID_and_slice.y = xyz.z;
    
    return output;
}
[maxvertexcount(1)]
void GS( point VS_OUTPUT input[1], inout PointStream<GS_OUTPUT> triStream )
{
	GS_OUTPUT o;
	
	o.projCoord = input[0].projCoord;
	o.vertexID  = input[0].vertexID_and_slice.x;
	o.RTIndex  = input[0].vertexID_and_slice.y;
	
	triStream.Append(o);
	
}



uint PS(PS_INPUT o) : SV_TARGET0
{
  return o.vertexID + 1;
}
